package singleLinked;

public class Test {
    public static int length(SingleLinkedList.ListNode cur) {
        if (cur ==null) {
            return 0;
        }
        int len=0;
        while (cur!=null) {
            len++;
            cur = cur.next;
        }
        return len;
    }
    public static SingleLinkedList.ListNode getIntersectionNode(SingleLinkedList.ListNode headA, SingleLinkedList.ListNode headB) {
        if (headA==null || headB==null) {
            return null;
        }
        int list1 = length(headA);
        int list2 = length(headB);
        int ret = Math.abs(list1-list2);
        if (list1>list2) {
            while(ret!=0) {
                headA=headA.next;
                ret--;
            }
        } else {
            while(ret!=0) {
                headB=headB.next;
                ret--;
            }

        }
        while(headA!=null && headB!=null) {
            if (headA==headB) {
                return headA;
            }

            headA=headA.next;
            headB=headB.next;

        }
        return null;
    }
    public static void main(String[] args) {

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addLast(4);
        singleLinkedList.addLast(1);
        singleLinkedList.addLast(8);
        singleLinkedList.addLast(4);
        singleLinkedList.addLast(5);
        SingleLinkedList singleLinkedList1 = new SingleLinkedList();
        singleLinkedList1.addLast(5);
        singleLinkedList1.addLast(6);
        singleLinkedList1.addLast(1);
        singleLinkedList1.head.next.next.next=singleLinkedList.head.next.next;
        SingleLinkedList.ListNode ret =getIntersectionNode(singleLinkedList.head,singleLinkedList1.head);

    }
    public static void main2(String[] args) {
        //测试逆序打印：
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addLast(1);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(4);
        singleLinkedList.display();
        singleLinkedList.displayRecu(singleLinkedList.head);
    }

    public static void main1(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //头插
        System.out.println("====头插====");
        singleLinkedList.addFirst(1);
        singleLinkedList.addFirst(1);
        singleLinkedList.addFirst(1);
        singleLinkedList.display();
        //尾插
        System.out.println("====尾插====");
        singleLinkedList.addLast(555);
        singleLinkedList.addLast(666);
        singleLinkedList.addLast(777);
        singleLinkedList.display();
        //指定下标插入
        System.out.println("====指定下标插入====");
        singleLinkedList.addIndex(6,1);
        singleLinkedList.display();
        //查找是否包含关键字key是否在单链表当中
        System.out.println("查找是否包含关键字key是否在单链表当中");
        System.out.println(singleLinkedList.contains(666));
        System.out.println(singleLinkedList.contains(8));
        //删除第一次出现关键字为key的节点
        System.out.println("删除第一次出现关键字为key的节点");
        singleLinkedList.remove(666);
        singleLinkedList.remove(1);
        singleLinkedList.display();
        //删除所有关键字为key的节点
        System.out.println("删除所有关键字为key的节点");
        singleLinkedList.removeAllKey(1);
        singleLinkedList.display();
        //清空链表
        System.out.println("清空链表");
        singleLinkedList.clear();
        singleLinkedList.display();

    }
}
